package com.dmlll.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmlll.domain.Purchase;
import com.dmlll.domain.PurchaseDrupDetail;
import com.dmlll.mapper.PurchaseDrupDetailMapper;
import com.dmlll.mapper.PurchaseMapper;
import com.dmlll.service.PurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@Service
@Transactional
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {
    @Autowired
    private PurchaseDrupDetailMapper purchaseDrupDetailMapper;
    @Override
    public boolean removeById(Serializable id) {
        QueryWrapper<PurchaseDrupDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_id",id);
        List<PurchaseDrupDetail> list = purchaseDrupDetailMapper.selectList(queryWrapper);
        List<Integer> ids = list.stream().map((pdd)->pdd.getId()).collect(Collectors.toList());
        purchaseDrupDetailMapper.deleteBatchIds(ids);
        return super.removeById(id);
    }

    @Override
    public Integer getNewPurchaseId() {
        return this.getBaseMapper().selectNewPurchaseId();
    }
}
